package com.apes.framework.plugin.condition.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.condition.model.ConditionTypes;
import com.apes.framework.plugin.condition.repository.ConditionTypesRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConditionTypesService extends DomainService {

    @Autowired
    ConditionTypesRepository conditionTypesRepository;


    /***
     * 功能： 创建
     * @param request
     * @return
     */
    public ConditionTypes create(SimpleRequest request) {
        ConditionTypes accessSequences = request.getO(ConditionTypes.class);
        return conditionTypesRepository.saveAndFlush(accessSequences);
    }

    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public ConditionTypes update(SimpleRequest request) {
        checkData(request);
        ConditionTypes accessSequences = request.getO(ConditionTypes.class);
        return conditionTypesRepository.saveAndFlush(accessSequences);
    }

    /***
     * 功能： 删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        checkData(request);
        ConditionTypes conditionField = request.getO(ConditionTypes.class);
        conditionTypesRepository.delete(conditionField);
        return "删除成功";
    }

    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        ConditionTypes oldConditionField = conditionTypesRepository.findOne(id);
        if (oldConditionField == null) throw new RuntimeException("条件类型【" + id + "】在系统中不存在。");
    }
}
